/* Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef __L_CERTIFICATE_H__
#define __L_CERTIFICATE_H__

#include <string_view>
#include <lua.hpp>
#include "luawrapper.h"
#include "common.h"
#include "certificate/certificate.h"

class LCertificate {
public:
    uint32_t         certificateType;
    uint32_t         certificateUsageType;
    string           fingerPrint;
    string           fingerPrintHashAlgorithm;
    string           issuer;
    string           subject;
    uint32_t         keyUsage;
    string           keyUsageString;
    string           serialNumber;
    string           signatureAlgorithm;
    uint64_t         validNotBefore;
    uint64_t         validNotAfter;
    string           commonName;
    uint32_t         certCount;
    uint32_t         keyLength;
    string           hashVal;

    LCertificate(Certificate *cert)
    {
        certificateType          = cert->certificateType;
        certificateUsageType     = cert->certificateUsageType;
        fingerPrint              = cert->fingerPrint;
        fingerPrintHashAlgorithm = cert->fingerPrintHashAlgorithm;
        issuer                   = cert->issuer;
        subject                  = cert->subject;
        keyUsage                 = cert->keyUsage;
        keyUsageString           = cert->keyUsageString;
        serialNumber             = cert->serialNumber;
        signatureAlgorithm       = cert->signatureAlgorithm;
        validNotBefore           = cert->validNotBefore;
        validNotAfter            = cert->validNotAfter;
        certCount                = cert->certCount;
        keyLength                = cert->keyLength;
        commonName               = cert->commonName;
        hashVal                  = cert->hashVal;
    };
    ~LCertificate() = default;

    int32_t get_certificate_type() const
    {
        return this->certificateType;
    }

    int32_t get_certificate_usage_type() const
    {
        return this->certificateUsageType;
    }

    std::string_view get_finger_print() const
    {
        return this->fingerPrint;
    }

    uint32_t get_key_usage() const
    {
        return this->keyUsage;
    }

    std::string_view get_key_usage_str() const
    {
        return this->keyUsageString;
    }

    std::string_view get_finger_print_hash_algo() const
    {
        return this->fingerPrintHashAlgorithm;
    }

    std::string_view get_issuer() const
    {
        return this->issuer;
    }

    std::string_view get_subject() const
    {
        return this->subject;
    }

    std::string_view get_serial_number() const
    {
        return this->serialNumber;
    }

    std::string_view get_signature_algo() const
    {
        return this->signatureAlgorithm;
    }

    uint64_t get_valid_not_before() const
    {
        return this->validNotBefore;
    }

    uint64_t get_valid_not_after() const
    {
        return this->validNotAfter;
    }

    uint32_t get_cert_count() const
    {
        return this->certCount;
    }

    uint32_t get_key_length() const
    {
        return this->keyLength;
    }

    std::string_view get_common_name() const
    {
        return this->commonName;
    }

    std::string_view get_hash_val() const
    {
        return this->hashVal;
    }

    static void register_to(lua_State *L, luawrap::stack_table &t)
    {
        luawrap::restore_stack_top _s(L);

        auto cls = luawrap::lua_class<LCertificate>(L);
        cls.def_property("certificateType", c_func_wrap(L, LCertificate::get_certificate_type));
        cls.def_property("certificateUsageType", c_func_wrap(L, LCertificate::get_certificate_usage_type));
        cls.def_property("fingerPrint", c_func_wrap(L, LCertificate::get_finger_print));
        cls.def_property("keyUsage", c_func_wrap(L, LCertificate::get_key_usage));
        cls.def_property("keyUsageString", c_func_wrap(L, LCertificate::get_key_usage_str));
        cls.def_property("fingerPrintHashAlgorithm", c_func_wrap(L, LCertificate::get_finger_print_hash_algo));
        cls.def_property("issuer", c_func_wrap(L, LCertificate::get_issuer));
        cls.def_property("subject", c_func_wrap(L, LCertificate::get_subject));
        cls.def_property("serialNumber", c_func_wrap(L, LCertificate::get_serial_number));
        cls.def_property("signatureAlgorithm", c_func_wrap(L, LCertificate::get_signature_algo));
        cls.def_property("validNotBefore", c_func_wrap(L, LCertificate::get_valid_not_before));
        cls.def_property("validNotAfter", c_func_wrap(L, LCertificate::get_valid_not_after));
        cls.def_property("certCount", c_func_wrap(L, LCertificate::get_cert_count));
        cls.def_property("keyLength", c_func_wrap(L, LCertificate::get_key_length));
        cls.def_property("commonName", c_func_wrap(L, LCertificate::get_common_name));
        cls.def_property("hashVal", c_func_wrap(L, LCertificate::get_hash_val));

        t.set("Certificate", cls);
    }
};

#endif // __L_CERTIFICATE_H__